﻿
namespace ZquenceStudio3Host.DspPlugins.Synthesizers.Nodesizer.Engine.Modules.LowLevel
{
    [ModuleDisplay("Substraction")]
    class SubtactionModule : BaseModule
    {
        public readonly ModuleInput A;
        public readonly ModuleInput B;
        public readonly ModuleOutput Out;

        public SubtactionModule(ModuleManager manager)
            : base(manager)
        {
            // Add input and outputs to module.
            AddInput(A = new ModuleInput("A", IOType.AudioOrWaveform, this));
            AddInput(B = new ModuleInput("B", IOType.AudioOrWaveform, this));
            AddOutput(Out = new ModuleOutput("Out", IOType.AudioOrWaveform, this));
        }

        public override void Process()
        {
            // First check if both inputs sources is connected.
            if (A.IsConnected && B.IsConnected)
            {
                for (int i = 0; i < Out.AudioBuffer.Length; i++)
                {
                    Out.AudioBuffer[i] = A.FromOutput.AudioBuffer[i] - B.FromOutput.AudioBuffer[i];
                }
            }
            else if (A.IsConnected)
            {
                for (int i = 0; i < Out.AudioBuffer.Length; i++)
                {
                    Out.AudioBuffer[i] = A.FromOutput.AudioBuffer[i];
                }
            }
            else if (B.IsConnected)
            {
                for (int i = 0; i < Out.AudioBuffer.Length; i++)
                {
                    Out.AudioBuffer[i] = B.FromOutput.AudioBuffer[i];
                }
            }
        }
    }
}
